

/**
 * Module denpendencies
 */
var crypto = require('crypto');
var querystring = require('querystring');
var request = require('request-promise');

var URL = 'https://api.seniverse.com/v3/';

function Api (uid, secretKey) {
    this.uid = uid;
    this.secretKey = secretKey;
}

Api.prototype.getSignatureParams = function () {

    var params = {}
    params.ts = Math.floor((new Date()).getTime() / 1000); // 当前时间戳（秒）
    params.ttl = 300; // 过期时间
    params.uid = this.uid; // 用户ID

    var str = querystring.encode(params); // 构造请求字符串
    // 使用 HMAC-SHA1 方式，以 API 密钥（key）对上一步生成的参数字符串进行加密
    params.sig = crypto.createHmac('sha1', this.secretKey)
        .update(str)
        .digest('base64'); // 将加密结果用 base64 编码，并做一个 urlencode，得到签名 sig

    return params;
}

/**
 * 获取当前时间天气情况 - 天气实况
 * https://api.seniverse.com/v3/weather/now.json?key=your_api_key&location=beijing&language=zh-Hans&unit=c
 */
Api.prototype.getWeatherNow = function ({ location }) {
    var params = this.getSignatureParams();
    params.location = location;

    // 将构造的 URL 直接在后端 server 内调用
    return request({
        url: URL + 'weather/now.json',
        qs: params,
        rejectUnauthorized: false,
        json: true
    });
}
/**
 * 24小时逐小时天气预报
 * https://api.seniverse.com/v3/weather/hourly.json?key=your_api_key&location=beijing&language=zh-Hans&unit=c&start=0&hours=24
*/

Api.prototype.getWeatherHours = function ({ location, hours = 24, start = 0 }) {
    var params = this.getSignatureParams();
    params.location = location;
    params.hours = hours;
    params.start = start;
    return request({
        url: URL + 'weather/hourly.json',
        qs: params,
        rejectUnauthorized: false,
        json: true
    });
}

/**
 * 未来15天逐日天气预报和昨日天气
 * https://api.seniverse.com/v3/weather/daily.json?key=your_api_key&location=beijing&language=zh-Hans&unit=c&start=-1&days=5
*/

Api.prototype.getWeatherDaily = function ({ location, days = 3, start = 0 }) {
    var params = this.getSignatureParams();
    params.location = location;
    params.days = days;
    params.start = start;
    return request({
        url: URL + 'weather/daily.json',
        qs: params,
        rejectUnauthorized: false,
        json: true
    });
}

/**
 * 气象灾害预警
 * https://api.seniverse.com/v3/weather/alarm.json?key=your_api_key&location=beijing&detail=more
*/

Api.prototype.getWeatherAlarm = function ({ location, detail = 'more' }) {
    var params = this.getSignatureParams();
    params.location = location;
    params.detail = detail;
    return request({
        url: URL + 'weather/alarm.json',
        qs: params,
        rejectUnauthorized: false,
        json: true
    });
}
/**
 * 生活指数
 * https://api.seniverse.com/v3/life/suggestion.json?key=your_api_key&location=shanghai&language=zh-Hans
 */

Api.prototype.getWeatherLifeSuggestion = function ({ location }) {
    var params = this.getSignatureParams();
    params.location = location;
    return request({
        url: URL + 'life/suggestion.json',
        qs: params,
        rejectUnauthorized: false,
        json: true
    });
}
module.exports = Api;
